﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Editor.Elements;
using Editor.PageControls;

namespace Editor
{
    public partial class Canvas : Form
    {
        const int squareSize = 10;

        public Canvas()
        {
            InitializeComponent();
        }

        public bool SwitchToPage(Page page)
        {
            if (EditorForm.PreviousPage != null)
            {
                unloadPageFromCanvas(EditorForm.PreviousPage);
                EditorForm.PreviousPage = null;
            }
            else if (EditorForm.CurrentPage != null)
                unloadPageFromCanvas(EditorForm.CurrentPage);

            EditorForm.CurrentPage = page;
            loadPageToCanvas(page);

            if (!EditorForm.Copying)
            {
                PageControl.SelectedControl = null;

                if (EditorForm.PropertiesForm != null)
                {
                    EditorForm.PropertiesForm.Visible = false;
                    EditorForm.PropertiesForm = null;
                }
            }

            EditorForm.RefreshCanvas();

            return true;
        }

        public bool SwitchToSelectedPage()
        {
            return SwitchToPage(EditorForm.SelectedNode.Tag as Page);
        }

        public void RemoveControl(PageControl pageControl)
        {
            pageControl.Selected = false;
            panelCanvas.Controls.Remove(pageControl.InnerControl);
            EditorForm.CurrentPage.RemovePageControl(pageControl);
            EditorForm.UpdateControlList();
        }

        private void panelCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            PageControl pickedControl = EditorForm.ToolBoxPickedControl;

            if (pickedControl != null || EditorForm.Relocating || EditorForm.Copying)
            {
                panelCanvas.Cursor = Cursors.Cross;
            }
            else
                panelCanvas.Cursor = Cursors.Default;
        }

        private void panelCanvas_Paint(object sender, PaintEventArgs e)
        {
            drawRectangles(e.Graphics);
        }

        private void panelCanvas_MouseUp(object sender, MouseEventArgs e)
        {
            PageControl pickedControl = EditorForm.ToolBoxPickedControl;

            if (pickedControl != null)
            {
                pickedControl = addControlToCanvas(pickedControl, e.Location);
                EditorForm.CurrentPage.AddPageControl(pickedControl);
                EditorForm.ToolBoxPickedControl = null; 
            }

            if (PageControl.SelectedControl != null)
                PageControl.SelectedControl.Selected = false;

            if (EditorForm.Relocating)
            {
                PageControl.SelectedControl.Location = e.Location;
                PageControl.SelectedControl.UpdateInnerControl();
                Refresh();
                EditorForm.Relocating = false;
                EditorForm.IsSaved = false;
            }
            else if (EditorForm.Copying)
            {
                PageControl pc = PageControl.SelectedControl.Clone() as PageControl;

                pc.Name = EditorForm.GenerateName(pc.NamingPrefix);
                pc.Location = e.Location;
                EditorForm.CurrentPage.AddPageControl(pc);
                pc = addControlToCanvas(pc, e.Location);
                pc.UpdateInnerControl();
                Refresh();
                EditorForm.Copying = false;
                EditorForm.IsSaved = false;
            }

            EditorForm.UpdateControlList();
        }

        private void Canvas_FormClosed(object sender, FormClosedEventArgs e)
        {
            EditorForm.CanvasChecked = false;
        }

        private void Canvas_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (PageControl.SelectedControl != null)
                {
                    RemoveControl(PageControl.SelectedControl);
                    Refresh();
                }
            }
            else if (e.KeyCode == Keys.Escape)
            {
                EditorForm.Relocating = false;
            }
        }

        private void drawRectangles(Graphics g)
        {
            if (PageControl.SelectedControl != null && PageControl.SelectedControl.Selected)
            {
                PageControl pc = PageControl.SelectedControl;

                Rectangle upLeft = new Rectangle(pc.Location.X - squareSize / 2,
                    pc.Location.Y - squareSize / 2, squareSize, squareSize);
                Rectangle upMiddle = new Rectangle(pc.Location.X + pc.Size.Width / 2 - squareSize / 2,
                    pc.Location.Y - squareSize / 2, squareSize, squareSize);
                Rectangle upRight = new Rectangle(pc.Location.X + pc.Size.Width - squareSize / 2,
                    pc.Location.Y - squareSize / 2, squareSize, squareSize);
                Rectangle downLeft = new Rectangle(pc.Location.X - squareSize / 2,
                    pc.Location.Y + pc.Size.Height - squareSize / 2, squareSize, squareSize);
                Rectangle downMiddle = new Rectangle(pc.Location.X + pc.Size.Width / 2 - squareSize / 2,
                    pc.Location.Y + pc.Size.Height - squareSize / 2, squareSize, squareSize);
                Rectangle downRight = new Rectangle(pc.Location.X + pc.Size.Width - squareSize / 2,
                    pc.Location.Y + pc.Size.Height - squareSize / 2, squareSize, squareSize);

                g.DrawRectangles(Pens.Black, new Rectangle[] { upLeft, upMiddle, upRight, 
                    downLeft, downMiddle, downRight });
                
            }
        }

        private void loadPageToCanvas(Page page)
        {
            foreach (PageControl control in page.Controls)
            {
                control.UpdateInnerControl();
                panelCanvas.Controls.Add(control.InnerControl);
                control.InnerControl.BringToFront();
            }

            Text = page.CurrentName;

            EditorForm.UpdateControlList();
        }

        private void unloadPageFromCanvas(Page page)
        {
            foreach (PageControl control in page.Controls)
                panelCanvas.Controls.Remove(control.InnerControl);
        }

        private PageControl addControlToCanvas(PageControl pc, Point location)
        {
            pc.Location = location;
            pc.UpdateInnerControl();
            panelCanvas.Controls.Add(pc.InnerControl);
            pc.InnerControl.BringToFront();

            return pc;
        }
    }
}
